package com.lefeng.cockpit.datax.tool.query;

import com.lefeng.cockpit.common.bean.dto.datasource.DataSourceDTO;
import com.lefeng.cockpit.common.constant.JdbcConstants;
import com.lefeng.cockpit.common.exception.RdbmsException;

import java.sql.SQLException;

/**
 * 工具类，获取单例实体
 *
 * @author weifw
 */
public class QueryToolFactory {

    public static BaseQueryTool getByDbType(DataSourceDTO dataSourceDTO) throws RdbmsException {
        //获取dbType
        String datasource = dataSourceDTO.getDsType().toLowerCase();
        switch (datasource) {
            case JdbcConstants.MYSQL:
                return getMySQLQueryToolInstance(dataSourceDTO);
            case JdbcConstants.ORACLE:
                return getOracleQueryToolInstance(dataSourceDTO);
            case JdbcConstants.POSTGRESQL:
                return getPostgresqlQueryToolInstance(dataSourceDTO);
            case JdbcConstants.SQL_SERVER:
                return getSqlserverQueryToolInstance(dataSourceDTO);
            case JdbcConstants.HIVE:
                return getHiveQueryToolInstance(dataSourceDTO);
            case JdbcConstants.CLICKHOUSE:
                return getClickHouseQueryToolInstance(dataSourceDTO);
            case JdbcConstants.HBASE20XSQL:
                return getHbase20XsqlQueryToolQueryToolInstance(dataSourceDTO);
        }
        throw new UnsupportedOperationException("找不到该类型: ".concat(datasource));
    }

    private static BaseQueryTool getMySQLQueryToolInstance(DataSourceDTO jdbcDatasource) throws RdbmsException{
        try {
            return new MySQLQueryTool(jdbcDatasource);
        } catch (Exception e) {
            throw RdbmsException.asConnException(JdbcConstants.MYSQL,
                    e,jdbcDatasource.getUsername(),jdbcDatasource.getDsName());
        }
    }

    private static BaseQueryTool getOracleQueryToolInstance(DataSourceDTO jdbcDatasource) {
        try {
            return new OracleQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw RdbmsException.asConnException(JdbcConstants.ORACLE,
                    e,jdbcDatasource.getUsername(),jdbcDatasource.getDsName());
        }
    }

    private static BaseQueryTool getPostgresqlQueryToolInstance(DataSourceDTO jdbcDatasource) {
        try {
            return new PostgresqlQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw RdbmsException.asConnException(JdbcConstants.POSTGRESQL,
                    e,jdbcDatasource.getUsername(),jdbcDatasource.getDsName());
        }
    }

    private static BaseQueryTool getSqlserverQueryToolInstance(DataSourceDTO jdbcDatasource) {
        try {
            return new SqlServerQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw RdbmsException.asConnException(JdbcConstants.SQL_SERVER,
                    e,jdbcDatasource.getUsername(),jdbcDatasource.getDsName());
        }
    }

    private static BaseQueryTool getHiveQueryToolInstance(DataSourceDTO jdbcDatasource) {
        try {
            return new HiveQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw RdbmsException.asConnException(JdbcConstants.HIVE,
                    e,jdbcDatasource.getUsername(),jdbcDatasource.getDsName());
        }
    }
    private static BaseQueryTool getClickHouseQueryToolInstance(DataSourceDTO jdbcDatasource) {
        try {
            return new ClickHouseQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw RdbmsException.asConnException(JdbcConstants.CLICKHOUSE,
                    e, jdbcDatasource.getUsername(), jdbcDatasource.getDsName());
        }
    }

    private static Hbase20XsqlQueryTool getHbase20XsqlQueryToolQueryToolInstance(DataSourceDTO jdbcDatasource) {
        try {
            return new Hbase20XsqlQueryTool(jdbcDatasource);
        } catch (SQLException e) {
            throw RdbmsException.asConnException(JdbcConstants.HBASE20XSQL,
                    e, jdbcDatasource.getUsername(), jdbcDatasource.getDsName());
        }
    }
}
